package com.guodidi.media.file;

import lombok.Data;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

/**
 * 自定义的文件遍历器 Created by 郭垚辉 on 2017/7/28.
 */
@Data
public class CustomFileVisitor implements FileVisitor<Path> {

    private List<Path> pathList;
    private CustomVisitResult result;

    public CustomFileVisitor(List<Path> pathList, CustomVisitResult result) {
        this.pathList = pathList;
        this.result = result;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        return result.getPreResult();
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (Files.isRegularFile(file)) {
            pathList.add(file);
        }
        return result.getVisitorResult();
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        return result.getFailedResult();
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        return result.getPostResult();
    }
}
